<?php namespace Common\Model;
use Hdphp\Model\Model;
use \Common\Model\Goods_detail;
use \Common\Model\Goods_attr;
use \Common\Model\Goods_list;
class Goods extends Model{
	//指定表
	protected $table="goods";
	//自动验证
	protected $validate=array(
		array('gname','required','商品名称不能为空',3,3)
	);
	//自动完成
	protected $auto=array(
		array('time','time','function',3,1)
	);
	//添加商品
	public function store(){
		if(!$this->check()) return FALSE;
		//先添加goods表，并获取gid
		$gid=$this->add();
		//再添加goods_detail表
		$goods_detailModel=new Goods_detail;
		$goods_detailModel->create();
		$dimg=Q('post.dimg');
		if($dimg){
			$dimg=implode('|', $dimg);
			$goods_detailModel->data['dimg']=$dimg;
		}
		$goods_detailModel->data['shop_goods_gid']=$gid;
		$goods_detailModel->add();
		//最后添加goods_attr表
		$goods_attrModel=new Goods_attr;
		//属性添加
		foreach(Q('post.attr') as $taid=>$v){
			if($v){
				$data=array(
					'gavalue'=>$v,
					'shop_type_attr_taid'=>$taid,
					'shop_goods_gid'=>$gid
				);
				$goods_attrModel->add($data);
			}
		}		
		//规格添加
		$spec = Q('post.spec');
		if($spec){
			$value=$spec['value'];
			foreach($value as $taid=>$v){
				foreach($v as $kk=>$vv){
					if($vv){
						$data=array(
							'gavalue'=>$vv,
							'shop_type_attr_taid'=>$taid,
							'shop_goods_gid'=>$gid,
							'gaadd'=>$spec['gaadd'][$taid][$kk]
						);
						$goods_attrModel->add($data);
					}
				}
			}
		}
		return TRUE;
	}
	//编辑商品
	public function edit(){
		if(!$this->check()) return FALSE;
		//修改商品表获取gid
		$pic=Q("post.pic");
		if(!$pic){$this->data['pic']=$pic;}
		$this->save();
		$gid=Q('post.gid');
		//修改商品详情表
		$goods_detailModel=new Goods_detail;
		$goods_detailModel->create();
		$dimg=Q("post.dimg");
		if($dimg){$dimg=implode('|', $dimg);}
		$goods_detailModel->data['dimg']=$dimg;
		$goods_detailModel->data['shop_goods_gid']=$gid;
		$goods_detailModel->save($goods_detailModel->data);
		//修改商品属性表
		$goods_attrModel=new Goods_attr;
		//删除原有属性
		$goods_attrModel->where("shop_goods_gid={$gid}")->delete();
		//删除相关属性的货品表
		$goods_listModel=new Goods_list;
		$goods_listModel->where("shop_goods_gid={$gid}")->delete();
		//重新添加属性
		foreach(Q('post.attr')as $taid=>$v ){
			if($v){
				$data=array(
					'gavalue'=>$v,
					'shop_goods_gid'=>$gid,
					'shop_type_attr_taid'=>$taid
				);
				$goods_attrModel->add($data);
			}
		}
		//重新添加规格
		$spec=Q('post.spec');
		if($spec){
			$value=$spec['value'];
			foreach($value as $taid=>$v){
				foreach($v as $kk=>$vv){
					if($vv){
						$data=array(
							'gavalue'=>$vv,
							'shop_goods_gid'=>$gid,
							'shop_type_attr_taid'=>$taid,
							'gaadd'=>$spec['gaadd'][$taid][$kk]
						);
						$goods_attrModel->add($data);
					}
				}
			}
		}
		return TRUE;
	}
	//验证三张表
	public function check(){
		//验证商品表
		if(!$this->create()) return FALSE;
		//判断错误信息，则上传失败
		if($this->error) return FALSE;
		//验证商品详情表
		$goods_detailModel=new Goods_detail;
		if(!$goods_detailModel->create()){
			$this->error=$goods_detailModel->getError();
			return FALSE;
		}
		return TRUE;
	}
}
 ?>